package com.newcoin.chat.service.impl;

import com.newcoin.chat.beans.entity.UserEntity;
import com.newcoin.chat.beans.vo.ResultVo;
import com.newcoin.chat.beans.vo.UserVo;
import com.newcoin.chat.dao.UserDao;
import com.newcoin.chat.service.UserService;
import com.newcoin.chat.util.QRCodeUtil;
import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao dao;

    //        private static final String HOST="http://127.0.0.1";//devip
//    private static final String HOST = "http://192.168.0.103";//生产及测试ip
//    private static final String HOST = "http://111.229.50.67";//线上测试ip

    @Value("${host}")
    private String host;
    @Value("${qrcode}")
    private String qrcode;

    /**
     * 是否授权
     *
     * @param superUserId
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo<UserVo> whetherAuthorized(long superUserId) throws Exception {
        log.info("是否授权入参 superUserId={}", superUserId);
        ResultVo<UserVo> resultVo = new ResultVo<>();
        UserVo userVo = dao.whetherAuthorized(superUserId);
        log.info("userVo={}", userVo);
        if (userVo == null) {
            resultVo.setStatus(401);//未授权
        } else {
            resultVo.setStatus(200);
            resultVo.setResultData(userVo);
        }
        log.info("resultVo={}", resultVo);
        return resultVo;
    }

    /**
     * 授权 同步信息
     *
     * @param superUserId
     * @param mobile
     * @param nickname
     * @param headImgPath
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo<UserVo> authorized(long superUserId, String mobile, String nickname, String headImgPath) throws Exception {
        log.info("授权 同步信息入参 superUserId={},mobile={},nickname={},headImgPath={}", superUserId, mobile, nickname, headImgPath);
        ResultVo<UserVo> resultVo = new ResultVo<>();
        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(String.valueOf((System.currentTimeMillis() / 1000L) * (int) (Math.random() * 9 + 1)));
        if (headImgPath == null || headImgPath.equalsIgnoreCase(""))
            userEntity.setHeadImgPath(host + ":23543/static/default/head/default.jpg");
        else
            userEntity.setHeadImgPath(headImgPath);
        userEntity.setMobile(mobile);
        userEntity.setNickname(nickname == null ? mobile : nickname);
        userEntity.setSuper_user_id(superUserId);
        userEntity.setCircleOfFriendsImg(host + ":23543/static/default/circleOfFriend/default.png");
        int row = dao.authorized(userEntity);
        if (row == 0) {
            resultVo.setStatus(509);
            resultVo.setMsg("授权失败");
        } else {
            UserVo userVo = new UserVo();
            userVo.setAccount(userEntity.getAccount());
            userVo.setMobile(userEntity.getMobile());
            userVo.setState(userEntity.getState());
            userVo.setHeadImgPath(userEntity.getHeadImgPath());
            userVo.setCircleOfFriendsImg(userEntity.getCircleOfFriendsImg());
            userVo.setId(userEntity.getId());
            userVo.setNickname(userEntity.getNickname());
            resultVo.setResultData(userVo);
            resultVo.setStatus(200);
        }
        new Thread(() -> {
            int row1 = dao.saveQRCode(userEntity.getId(), host + ":23543/static/qrcode/" + userEntity.getId() + ".png");
//            QRCodeUtil.generateQRCodeImage(String.valueOf(userEntity.getId()), 350, 350,"D:\\static\\qrcode\\");
            QRCodeUtil.generateQRCodeImage(String.valueOf(userEntity.getId()), 350, 350, qrcode);
            log.info("添加二维码路径 row1={}", row1);
        }).start();
        log.info("resultVo={}", resultVo);
        return resultVo;
    }

    /**
     * 通过Id获取用户信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo<UserVo> getUserInfoById(long userId) throws Exception {
        log.info("userId={}", userId);
        ResultVo<UserVo> resultVo = new ResultVo<>();
        UserVo userVo = dao.getUserInfoById(userId);
        log.info("userVo={}", userVo);
        resultVo.setStatus(200);
        resultVo.setMsg("查询成功");
        resultVo.setResultData(userVo);
        log.info("resultVo={}", resultVo);
        return resultVo;
    }

    /**
     * 修改头像
     *
     * @param userId
     * @param headPath
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo updateUserHead(Long userId, String headPath) throws Exception {
        log.info("修改头像入参userId={},headPath={}", userId, headPath);
        ResultVo resultVo = new ResultVo();
        if (headPath == null || headPath.equalsIgnoreCase("")) {
            resultVo.setStatus(404);
            resultVo.setMsg("上传失败 无效路径");
            return resultVo;
        }
        int row = dao.updateUserHead(userId, headPath);
        if (row != 1) {
            resultVo.setStatus(404);
            resultVo.setMsg("上传失败");
            return resultVo;
        }
        resultVo.setStatus(200);
        resultVo.setMsg("上传成功");
        return resultVo;
    }

    /**
     * 修改昵称
     *
     * @param userId
     * @param nickname
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo updateUserNickname(Long userId, String nickname) throws Exception {
        log.info("修改昵称入参 userId={},nickname={}", userId, nickname);
        ResultVo resultVo = new ResultVo();
        int row = dao.updateUserNickname(userId, nickname);
        if (row != 1) {
            resultVo.setStatus(500);
            resultVo.setMsg("修改昵称失败");
            return resultVo;
        }
        resultVo.setStatus(200);
        resultVo.setMsg("成功");
        return resultVo;
    }


}
